/*
 * Copyright (c) 2001, 2002 Boris Pruessmann and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors:
 *    Boris Pruessmann - Initial implementation.
 */
package net.sourceforge.perforce.core.internal.api;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import net.sourceforge.perforce.api.IPerforceCommand;
import net.sourceforge.perforce.core.PerforceCorePlugin;
import net.sourceforge.perforce.core.api.IOpenedResult;
import net.sourceforge.perforce.core.api.IPerforceServer;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.MultiStatus;
import org.eclipse.core.runtime.PlatformObject;

/**
  * ClientUser object for the 'p4 opened' command.
  * 
  * @version $Revision: 18 $
  * @author  <a href="mailto:bpruessmann@users.sourceforge.net">Boris Pruessmann</a>
  */
public class Opened extends AbstractPerforceCommand implements IPerforceCommand
{
	// --------------------------------------------------------------------------- Private Variables

	/** Stores the operation's result. */
	private List result = new ArrayList();
	/** Stores the perforce server.
	  * TODO: Remove
	  */
	private IPerforceServer perforceServer;
	
	// --------------------------------------------------------------- Public Static Factory Methods
	
	/** Convenience method for creating instances of Opened.
	  * 
	  * @param perforceServer the perforce server object.
	  * @return the new Opened instance.
	  */
	public static final Opened newInstance(IPerforceServer perforceServer)
	{
		MultiStatus status = new MultiStatus(PerforceCorePlugin.ID, IStatus.OK, "", null); 
		return new Opened(status, perforceServer);
	}
	
	// ------------------------------------------------------------------------ Private Constructors
 
	/** Constructor for Opened.
	  * 
	  * @param status the MultiStatus instance.
	  * @param perforceServer the server.
	  */
	private Opened(MultiStatus status, IPerforceServer perforceServer)
	{		
		super(status);
		this.perforceServer = perforceServer;
	}

	// -------------------------------------------------------- Public Methods (IClientUserDelegate)

	/** @see IClientUserDelegate#outputStat(Map) */
	public void outputStat(Map strDict)
	{
		Result tempResult = new Result();
		tempResult.setDepotFilename(Helper.safeGetString(strDict, "depotFile"));       //$NON-NLS-1$
		tempResult.setClientFilename(Helper.safeGetString(strDict, "clientFile"));     //$NON-NLS-1$
		tempResult.setRevision(Helper.safeGetInteger(strDict, "rev"));                 //$NON-NLS-1$
		tempResult.setAction(Helper.safeGetString(strDict, "action"));                 //$NON-NLS-1$
		tempResult.setChangelist(Helper.safeGetChangelist(strDict, "change"));         //$NON-NLS-1$
		tempResult.setType(Helper.safeGetString(strDict, "type"));                     //$NON-NLS-1$
		tempResult.setUserName(Helper.safeGetString(strDict, "user"));                 //$NON-NLS-1$
		tempResult.setClientName(Helper.safeGetString(strDict, "client"));             //$NON-NLS-1$
		tempResult.setPerforceServer(perforceServer);
		
		result.add(tempResult);
	}
	
	// ----------------------------------------------------------- Public Methods (IPerforceCommand)
	
	/** @see IPerforceCommand#getCommand() */
	public String getCommand()
	{
		return "opened";                                                               //$NON-NLS-1$
	}

	/** @see IPerforceCommand#getStatus() */
	public IStatus getStatus()
	{
		return getMultiStatus();
	}
	
	/** @see IPerforceCommand#isHidingFromConsole() */
	public boolean isHidingFromConsole()
	{
		return false;
	}	
	
	// ------------------------------------------------------------------------------ Public Methods
	
	/** Returns the result of the operation.
	  * 
	  * @return array of result objects.
	  */
	public IOpenedResult[] getResult()
	{
		return (IOpenedResult[]) result.toArray(new IOpenedResult[result.size()]);
	}	

	// ---------------------------------------------------------------------- Private Nested Classes
	
	/**
	  * Implementation of the IOpenedResult. Stores information provided
	  * by the 'p4 opened' command.
	  * 
	  * @see IOpenedResult
	  * 
	  * TODO: Remove PlatformObject dependency.
	  */	
	private static class Result extends PlatformObject implements IOpenedResult
	{		
		// ----------------------------------------------------------------------- Private Variables
		
		/** Stores the action. */
		private String action;
		/** Stores the changelist number. */
		private Integer changelist;
		/** Stores the client name. */
		private String clientName;
		/** Stores the filename in client syntax. */
		private String clientFilename;
		/** Stores the filename in depot syntax. */
		private String depotFilename;
		/** Stores the revision number. */
		private Integer revision;
		/** Stores the filetype. */
		private String type;
		/** Stores the username. */
		private String userName;
		/** Stores the perforce server.
		  * TODO: Remove
		  */
		private IPerforceServer perforceServer;

		// -------------------------------------------------------------------------- Public Methods
		
		/** @see IOpenedResult#getAction() */
		public String getAction() 
		{ 
			return action; 
		}

		/** @see IOpenedResult#getChangelist() */
		public Integer getChangelist() 
		{ 
			return changelist; 
		}

		/** @see IOpenedResult#getClientName() */
		public String getClientName() 
		{ 
			return clientName; 
		}

		/** @see IOpenedResult#getClientSyntax() */
		public String getClientFilename() 
		{ 
			return clientFilename; 
		}

		/** @see IOpenedResult#getDepotFilename() */
		public String getDepotFilename() 
		{ 
			return depotFilename; 
		}

		/** @see IOpenedResult#getRevision() */
		public Integer getRevision()
		{ 
			return revision; 
		}

		/** @see IOpenedResult#getType() */
		public String getType()
		{ 
			return type; 
		}

		/** @see IOpenedResult#getUserName() */
		public String getUserName() 
		{ 
			return userName; 
		}

		/** @see IOpenedResult#getPerforceServer() */
		public IPerforceServer getPerforceServer() 
		{
			return perforceServer;
		}
		
		// --------------------------------------------------------------------------------- Methods

		/** Sets the action.
		  * 
		  * @param action The action to set
		  */
		void setAction(String action) 
		{
			this.action = action;
		}

		/** Sets the changelist.
		  * 
		  * @param changelist The changelist to set
		  */
		void setChangelist(Integer changelist) 
		{
			this.changelist = changelist;
		}

		/** Sets the clientFilename.
		  * 
		  * @param clientFilename The clientFilename to set
		  */
		void setClientFilename(String clientFilename) 
		{
			this.clientFilename = clientFilename;
		}

		/** Sets the clientName.
		  * 
		  * @param clientName The clientName to set
		  */
		void setClientName(String clientName) 
		{
			this.clientName = clientName;
		}

		/** Sets the depotFilename.
		  * 
		  * @param depotFilename The depotFilename to set
		  */
		void setDepotFilename(String depotFilename) 
		{
			this.depotFilename = depotFilename;
		}

		/** Sets the perforceServer.
		  * 
		  * @param perforceServer The perforceServer to set
		  */
		void setPerforceServer(IPerforceServer perforceServer) 
		{
			this.perforceServer = perforceServer;
		}

		/** Sets the revision.
		  * 
		  * @param revision The revision to set
		  */
		void setRevision(Integer revision) 
		{
			this.revision = revision;
		}

		/** Sets the type.
		  * 
		  * @param type The type to set
		  */
		void setType(String type) 
		{
			this.type = type;
		}

		/** Sets the userName.
		  * 
		  * @param userName The userName to set
		  */
		void setUserName(String userName) 
		{
			this.userName = userName;
		}
	}		
}
